package com.demo.shardingsphere.user.config.rule.impl;

import com.demo.shardingsphere.user.config.rule.DbConfig;
import com.demo.shardingsphere.user.config.rule.IShardingService;
import com.demo.shardingsphere.util.DataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("shardingService")
public class  ShardingServiceImpl implements IShardingService {


    @Override
    public String getDb(Long id) throws SQLException {
        Map<String,Object> map=getDbMap(id);
        String dbName=   map.get("db_name").toString() ;
        return dbName;
    }

    private Long getGroupId(Long id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        Map<String,Object> map=queryRunner.query("SELECT * from t_group  where start_id<=? and end_id>?",new MapHandler(),id,id);
        Long groupId= Long.parseLong(  map.get("group_id").toString());
        return groupId;
    }
    public Map<String,Object> getDbMap(Long id) throws SQLException {
        Long groupId=this.getGroupId(id);
        long hashValue=id%10;
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        Map<String,Object> map=queryRunner.query("SELECT * from t_db  where group_id=? and hash_value like ?",new MapHandler(),groupId,"%"+hashValue+"%");
        return map;
    }
    @Override
    public String getTable(Long id) throws SQLException {
        Map<String,Object> dbMap=getDbMap(id);
        String dbId=   dbMap.get("db_id").toString() ;

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        Map<String,Object> tableMap=queryRunner.query("SELECT * from t_table  where db_id=? and start_id<=? and end_id>?",new MapHandler(),dbId,id,id);
        String tableName=   tableMap.get("table_name").toString() ;
        return tableName;

    }

    @Override
    public List<DbConfig> getDbConfigList() throws SQLException {

        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        List<Map<String,Object>> list=queryRunner.query("select * from t_db",  new MapListHandler());
        List<DbConfig> ret=new ArrayList();
        if(list!=null){
            for(Map<String,Object> map:list){
                DbConfig dbConfig=new DbConfig();
                dbConfig.setDbName(map.get("db_name").toString());
                dbConfig.setDriverClassName(map.get("driver_class_name").toString());
                dbConfig.setUsername(map.get("username").toString());
                dbConfig.setPassword(map.get("password").toString());
                dbConfig.setUrl(map.get("url").toString());
                ret.add(dbConfig);
            }
        }

        return ret;
    }
}
